﻿using RestApp.BusinessLogic.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RestApp.BusinessLogic
{
	public class RolesLogic : IRolesLogic
	{
		public IEnumerable<Role> GetAllRoles()
		{
			using(var context = new RestAppEntities())
			{
				return context
					.Roles
					.ToList();
			}
		}

		public Role GetUserRole(string userLogin)
		{
			using(var context = new RestAppEntities())
			{
				var user = context.Users.FirstOrDefault(t => t.Login.ToUpper() == userLogin.ToUpper());
				if(user == null)
				{
					throw new InvalidOperationException("Пользователь с логином " + userLogin + " не найден");
				}
				return user.Role;
			}
		}
	}
}
